package com.example.javabasic.thread.classtwo.threadpermessage.demo2;

import java.util.Random;

public class CounterIncrement extends Thread {
    private volatile boolean terminated = false;
    private int counter= 0;
    private Random random = new Random(System.currentTimeMillis());

    @Override
    public void run() {
        try{
            while(!terminated){
                System.out.println(Thread.currentThread().getName()+" " +counter++);
                Thread.sleep(random.nextInt(1000));
            }
        } catch (InterruptedException e) {

        } finally{
            this.clean();
        }

    }
    //清理
    public void clean(){
        System.out.println("清理! 当前count->"+counter);
    }

    public void close(){
        this.terminated = true;
        this.interrupt();
    }
}
